package com.imooc.log.util

import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType}

/**
  * Created by zghgchao 2017/12/25 10:14
  * 访问日志转换（输入==>输出）工具类
  */
object AccessConvertUtil {

  //定义的输出字段
  val struct = StructType(
    Array(
      StructField("url", StringType), //课程URL
      StructField("cmsType", StringType), //课程类型：video / article
      StructField("cmsId", LongType), //课程编号
      StructField("traffic", LongType), //耗费流量
      StructField("ip", StringType), //ip信息
      StructField("city", StringType), //所在城市
      StructField("time", StringType), //访问时间
      StructField("day", StringType) //分区字段，天
    )
  )

  /**
    * 根据输入的每一行信息转换成输出的样式
    *
    * @param log 输入的每一行记录信息 ：2017-05-11 14:09:14 	http://www.imooc.com/video/4500	304	218.75.35.226
    */
  def parseLog(log: String) = {

    try {
      val splits = log.split("\t")

      val url = splits(1)
      //http://www.imooc.com/video/4500
      val traffic = splits(2).toLong
      val ip = splits(3)

      val domain = "http://www.imooc.com/"
      //主域名
      val cms = url.substring(url.indexOf(domain) + domain.length)
      val cmsTypeId = cms.split("/")
      var cmsType = ""
      var cmsId = 0L
      if (cmsTypeId.length > 1) {
        cmsType = cmsTypeId(0)
        cmsId = cmsTypeId(1).toLong
      }

      val city = IpUtils.getCity(ip)
      val time = splits(0)
      //2017-05-11 14:09:14
      val day = time.split(" ")(0).replace("-", "") //time.substring(0,10).replace("-","")

      //Row中的字段要和Struct中的字段对应三
      /**
        * StructField("url", StringType), //课程URL
        * StructField("cmsType", StringType), //课程类型：video / article
        * StructField("cmsId", LongType), //课程编号
        * StructField("traffic", LongType), //耗费流量
        * StructField("ip", StringType), //ip信息
        * StructField("city", StringType), //所在城市
        * StructField("time", StringType), //访问时间
        * StructField("day", StringType) //分区字段，天
        */
      Row(url, cmsType, cmsId, traffic, ip, city, time, day)
    } catch {
      case e: Exception => Row(0)
    }

  }

  def main(args: Array[String]): Unit = {
    val url = "http://www.imooc.com/video/4500"

    val domain = "http://www.imooc.com/" //主域名

    val index_0 = url.indexOf(domain)
    val index_1 = index_0 + domain.length

    val cms = url.substring(index_1)

    val cmsTypeId = cms.split("/")
    var cmsType = ""
    var cmsId = 0L
    if (cmsTypeId.length > 1) {
      cmsType = cmsTypeId(0)
      cmsId = cmsTypeId(1).toLong
    }
    println(cmsType + "   " + cmsId)

    val time = "2017-05-11 14:09:14"
    val day = time.split(" ")(0).replace("-", "")
    println(day)
  }
}
